<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="getDialog({type:1,pid:''})">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="getDialog({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="getDetail(row.id)">编辑</el-button>
              <el-button type="text" @click="delDialog(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="closeDialog">
      <!-- 表单内容 -->
      <el-form label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, getPermission, updatePermission, delPermission } from '@/api/permission'
import treeArray from '@/utils/transTree'
export default {
  name: 'Permission',
  data() {
    const getcode = (rule, value, callback) => {
      const datalist = this.datalist.map(item => item.code)
      if (datalist.includes(value)) {
        callback(new Error('重复了换一个'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      datalist: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        code: [
          { trigger: 'blur', validator: getcode }
        ]
      }
    }
  },
  mounted() {
    this.GetPermissionList()
  },
  methods: {
    async  GetPermissionList() {
      const res = await getPermissionList()
      this.list = treeArray(res)
      this.datalist = res
    },
    // 打开弹窗
    getDialog({ type, pid }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.id = pid
    },
    // 关
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加
    async submit() {
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }
      this.closeDialog()
      this.GetPermissionList()
    },
    // 删除
    delDialog(id) {
      this.$confirm('确定要删除该权限吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.GetPermissionList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    // 编辑
    async  getDetail(id) {
      const res = await getPermission(id)
      this.formData = res
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
